home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 9 / CDACTUAL9.iso / share / Dos / VARIOS / pascal / DELPHI.SWG / 0010_Recently Used Files List.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-02-21  |  5.9 KB  |  248 lines

  1. unit MRUFList;
  2.  
  3. (*    Implements a Recently-Used Files List     *)
  4.  
  5. (*  Constructed by Robert R. Marsh, S.J., 1995  *)
  6. (* Use freely, distribute widely, charge nothing*)
  7. (* If you like it you could always give some    *)
  8. (* money to your favorite charity.              *)
  9.  
  10. (*  Comments, bug-reports, praise and blame to: *)
  11. (*              RobMarsh@AOL.COM                *)
  12.  
  13. interface
  14.  
  15. uses
  16.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  17.   Forms, Dialogs, Menus, IniFiles;
  18.  
  19. type
  20.   TRecentFileEvent = procedure(Sender: TObject; LatestFile: string) of object;
  21.  
  22. type
  23.   TRecentFiles = class(TComponent)
  24.   private
  25.     FMenu       : TMenuItem;
  26.     Divider     : TMenuItem;
  27.     FMaxFiles   : integer;
  28.     FIniFileName: string;
  29.     FLatestFile : string;
  30.     FOnClick    : TRecentFileEvent;
  31.     procedure SetLatestFile(value: string);
  32.     procedure SetMenu(value: TMenuItem);
  33.     procedure SetMaxFiles(value: integer);
  34.     procedure MenuOnClick(Sender: TObject);
  35.     function DividerPlace: integer;
  36.   protected
  37.     procedure Click(RecentFile: string);
  38.   public
  39.     constructor Create(AOwner: TComponent); override;
  40.     procedure SaveToIniFile;
  41.     procedure LoadFromIniFile;
  42.     property LatestFile: string read FLatestFile write SetLatestFile;
  43.   published
  44.     property Menu       : TMenuItem read FMenu write SetMenu;
  45.     property MaxFiles   : integer read FMaxFiles write SetMaxFiles;
  46.     property IniFileName: string read FIniFileName write FIniFileName;
  47.     property OnClick    : TRecentFileEvent read FOnClick write FOnClick;
  48.   end;
  49.  
  50. procedure Register;
  51.  
  52. implementation
  53.  
  54. procedure Register;
  55. begin
  56.   RegisterComponents('Mine', [TRecentFiles]);
  57. end;
  58.  
  59. {we frequently need to know both if the Divider}
  60. {has been added and, if so, where it is}
  61. function TRecentFiles.DividerPlace: integer;
  62. begin
  63. Result:=-1;
  64. if FMenu <> nil then
  65.   begin
  66.   Result:=Menu.IndexOf(Divider);
  67.   end;
  68. end;
  69.  
  70. procedure TRecentFiles.SetMenu(value: TMenuItem);
  71. begin
  72. FMenu:=value;
  73. end;
  74.  
  75. procedure TRecentFiles.SetMaxFiles(value: integer);
  76. var
  77.   n: integer;
  78. begin
  79. {the Max value of MaxFiles is 9}
  80. if value <=9 then
  81.   begin
  82.   FMaxFiles:=value;
  83.   end
  84. else
  85.   begin
  86.   FMaxFiles:=9;
  87.   end;
  88. if (FMenu <> nil) and (DividerPlace <> -1) then
  89.   begin
  90.   {trim off any entries more MaxFiles}
  91.   while Menu.Count > DividerPlace + FMaxFiles + 1 do
  92.     begin
  93.     Menu.Delete(Menu.Count-1);
  94.     end;
  95.   {if neccesary delete the divider too}
  96.   if FMaxFiles = 0 then
  97.     begin
  98.     Menu.Delete(Menu.Count-1);
  99.     end;
  100.   end;
  101. end;
  102.  
  103. procedure TRecentFiles.SetLatestFile(value: string);
  104. var
  105.   NewMenuItem: TMenuItem;
  106.   n          : integer;
  107.   Thiscaption: string;
  108.   OldPlace   : integer;
  109.   DividerPos : integer;
  110. begin
  111. FLatestFile:=value;
  112. if (Menu <> nil) and (MaxFiles > 0) and
  113.    (FLatestFile <> '') then
  114.   begin
  115.   {special case - the divider}
  116.   if DividerPlace < 0 then
  117.     begin
  118.     Menu.Add(Divider);
  119.     end;
  120.   {is the new Name already there?}
  121.   DividerPos:=DividerPlace;
  122.   n:=DividerPos+1;
  123.   while (n < Menu.Count) do
  124.     begin
  125.     if FLatestFile = Copy(Menu.Items[n].Caption,4,high(string)) then
  126.       begin
  127.       OldPlace:=n;
  128.       Break;
  129.       end
  130.     else
  131.       begin
  132.       inc(n);
  133.       end;
  134.   end;
  135.   if n >= Menu.Count then {we add}
  136.     begin
  137.     NewMenuItem:=TMenuItem.Create(Self);
  138.     NewMenuItem.Caption:='&1 '+FLatestFile;
  139.     {what happens if we click it}
  140.     NewMenuItem.OnClick:=MenuOnClick;
  141.     Menu.Insert(DividerPos+1,NewMenuItem);
  142.     end
  143.   else                 {we insert}
  144.     begin
  145.     NewMenuItem:=Menu.Items[OldPlace];
  146.     Menu.Delete(OldPlace);
  147.     Menu.Insert(DividerPos+1,NewMenuItem);
  148.     end;
  149.   {now change the 'hot' keys}
  150.   for n:=DividerPos+1 to Menu.Count - 1 do
  151.     begin
  152.     ThisCaption:=Menu.Items[n].Caption;
  153.     ThisCaption[2]:=Chr(n - DividerPos + Ord('1') -1);
  154.     Menu.Items[n].Caption:=ThisCaption;
  155.     end;
  156.   {delete any excess items}
  157.   if Menu.Count > DividerPos + MaxFiles + 1 then
  158.     begin
  159.     Menu.Delete(Menu.Count-1);
  160.     end;
  161.   end;
  162. end;
  163.  
  164. procedure TRecentFiles.Click(RecentFile: string);
  165. begin
  166. if Assigned(FOnClick) then FOnClick(Self,RecentFile);
  167. end;
  168.  
  169. procedure TRecentFiles.SaveToIniFile;
  170. var
  171.   IniFile   : TIniFile;
  172.   n         : integer;
  173.   DividerPos: integer;
  174. begin
  175. if Menu <> nil then
  176.   begin
  177.   {if this property is blank we use the default}
  178.   if IniFileName = '' then
  179.     begin
  180.     IniFileName:=ChangeFileExt(ExtractFileName(Application.ExeName),'.INI')
  181.     end;
  182.   IniFile:=TIniFile.Create(IniFileName);
  183.   IniFile.EraseSection('FileHistory');
  184.   IniFile.WriteInteger('FileHistory','MaxFiles',MaxFiles);
  185.   if (Menu <> nil) and (DividerPlace <> -1) then
  186.     begin
  187.     DividerPos:=DividerPlace;
  188.     n:=DividerPos+1;
  189.     while n < Menu.Count do
  190.       begin
  191.       IniFile.WriteString('FileHistory','File'+Chr(n+Ord('1')-1-DividerPos),Copy(Menu.Items[n].Caption,4,high(string)));
  192.       inc(n);
  193.       end;
  194.     IniFile.Free;
  195.     end;
  196.   end;
  197. end;
  198.  
  199. procedure TRecentFiles.LoadFromIniFile;
  200. var
  201.   IniFile: TIniFile;
  202.   n      : integer;
  203.   Name   : string;
  204. begin
  205. if Menu <> nil then
  206.   begin
  207.   if IniFileName = '' then
  208.     begin
  209.     IniFileName:=ChangeFileExt(ExtractFileName(Application.ExeName),'.INI')
  210.     end;
  211.   IniFile:=TIniFile.Create(IniFileName);
  212.   MaxFiles:=0;
  213.   MaxFiles:=IniFile.ReadInteger('FileHistory','MaxFiles',MaxFiles);
  214.   n:=1;
  215.   while true do
  216.     begin
  217.     Name:=IniFile.ReadString('FileHistory','File'+Chr(n+Ord('1')-1),'');
  218.     if Name = '' then
  219.       begin
  220.       Break;
  221.       end;
  222.     LatestFile:=Name;
  223.     inc(n);
  224.     end;
  225.   end;
  226. end;
  227.  
  228. constructor TRecentFiles.Create(AOwner: TComponent);
  229. begin
  230. inherited Create(AOwner);
  231. FMaxFiles:=0;
  232. FMenu:=nil;
  233. Divider:=TMenuItem.Create(Self);
  234. Divider.Caption:='-';
  235. end;
  236.  
  237. procedure TRecentFiles.MenuOnClick(Sender: TObject);
  238. var
  239.   Name: string;
  240. begin
  241.   begin
  242.   Name:=Copy(TMenuItem(Sender).Caption,4,high(string));
  243.   Click(Name);
  244.   end;
  245. end;
  246.  
  247. end.
  248.